<?php
/**
 * @file collection.php
 * @author LJ (liangjian@haibao.com)
 * @date 2015年11月25日 上午11:41:19
 * @brief 
 */
namespace haibao\app\business;

use haibao\frame\data\query\Condition;
class Collection extends \haibao\app\business\Base {

    /**
     * @var $_instance
     */
    private static $_instance;
    
    /**
     * instance()
     */
    private static function instance(){
        if(!self::$_instance){
            self::$_instance = new \haibao\app\data\Collection();
        }
        return self::$_instance;
    }
    
    /**
     * 收藏图片到相册
     */
    public static function collect($userId, $albumId, $imageId){
        if(!$userId || !$imageId || !$albumId){
            throw new BusinessException('参数不正确', self::DATA_ERROR_CODE_EMPTY);
        }
        /*
        if(!\haibao\app\business\Album::pregAlbum($userId, $albumId)){
            throw new BusinessException('用户与相册不匹配', self::DATA_ERROR_CODE_COLLECT_MISMATCH);
        }
        */
        if(self::getCollectionByImageIdArr($userId, (array)$imageId)){
            throw new BusinessException('已经收藏过该图片', self::DATA_ERROR_CODE_COLLECT_COLLECTED);
        }
        $image = \haibao\app\business\ImageLibrary::getImageById($imageId, true);
        $model = new \haibao\app\model\data\Collection();
        $model->Filename = $image['Filename'];
        $model->UserId = $userId;
        $model->Description = $image['Description'];
        $model->ImageId = $imageId;
        $model->AlbumId = $albumId;
        $model->CreateTime = new \DateTime();
        self::instance()->addCollect($model);
    }
    
    /**
     * 取消收藏
     */
    public static function deleteCollect($userId, $imageId){
        if(!$userId || !$imageId){
            throw new BusinessException('参数不正确', self::DATA_ERROR_CODE_EMPTY);
        }
        self::instance()->deleteCollect($userId, $imageId);
    }
    
    /**
     * 根据用户Id和图片Id查询图片详细信息
     */
    public static function getCollectionByImageIdArr($userId, $imageIdArr){
        if(!$userId || !$imageIdArr){
            throw new BusinessException('参数不正确', self::DATA_ERROR_CODE_EMPTY);
        }
        $imageIdArr = array_unique($imageIdArr);
        array_map('intval', $imageIdArr);
        return self::instance()->getCollectionByImageIdArr($userId, $imageIdArr);
    }
    
    /**
     * 查询是否收藏过图片
     */
    public static function isCollect($userId, $dividing){
        return (boolean)self::instance()->isCollect($userId, $dividing);
    }
    
    public static function getCollection($albumId, $cId = null, $pagesize = 30){
        if(!$albumId){
            throw new BusinessException('相册Id不能为空', self::DATA_ERROR_CODE_ALBUM_ID);
        }
        $query = new \haibao\app\model\filter\Collection();
        $query->select(array(
            $query::CONDITION_FIELD_NAME_ID,
            $query::CONDITION_FIELD_NAME_FILENAME,
            $query::CONDITION_FIELD_NAME_DESCRIPTION,
        ))
        ->where($query::CONDITION_FIELD_NAME_ALBUMID, \haibao\frame\data\query\Condition::CONDITION_EQUAL, $albumId)
        ->order($query::CONDITION_FIELD_NAME_ID, false)
        ->setPager(new \haibao\frame\data\query\Pager($pagesize, 1));
        
        if($cId){
            $query->where($query::CONDITION_FIELD_NAME_ID, Condition::CONDITION_LESS, $cId);
        }
        
        $image = new \haibao\app\model\filter\ImageLibrary();
        $image->select(array(
            $image::CONDITION_FIELD_NAME_ID,
            $image::CONDITION_FIELD_NAME_WIDTH,
            $image::CONDITION_FIELD_NAME_HEIGHT,
        ));
        // $image->where($image::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\app\model\data\ImageLibrary::STATUS_ENABLE);
        $query->leftJoin($image, null, $query::CONDITION_FIELD_NAME_IMAGEID, $image::CONDITION_FIELD_NAME_ID, $query::CONDITION_FIELD_NAME_IMAGEID, false);
        
        $imageList = self::instance()->getAll($query);        
        $retArr = array();
        if($imageList){
            foreach ($imageList as $key=>$image){
                $tmp['cId'] = $image->Id;
                $tmp['width'] = $image->ImageId->Width;
                $tmp['height'] = $image->ImageId->Height;
                $tmp['filename'] = $image->Filename;
                $tmp['imageDescription'] = $image->Description;
                $tmp['imageId'] = $image->ImageId->Id;
                array_push($retArr, $tmp);
            }
        }
        return $retArr;
    }
    
    public static function resetImageCount($userId, $albumId){
        return self::instance()->resetImageCount($userId, $albumId);
    }
} 